package cate.game.role.res.hero.introduction;

import cate.common.table.item.hero.row.HeroBaseRow;
import cate.common.table.res.hero.introduction.row.GainHeroRwdRow;
import cate.game.attr.FightAttr;
import cate.game.res.MixRes;
import cate.game.role.Role;
import cate.game.role.RoleDependent;
import cate.game.role.bag.hero.Hero;
import cate.game.role.res.hero.introduction.msg.*;
import com.fasterxml.jackson.annotation.JsonIgnore;
import easy.java.dev.note.NoteClass;
import easy.java.dev.note.NoteField;
import easy.java.practice.errorcode.EcResult;

import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.Map;

@NoteClass(value = "英雄图鉴")
public class HeroIntroduction extends RoleDependent {
  @NoteField(value = "玩家拥有过的食灵原型数据  Map<食灵tid，图鉴数据>")
  public Map<Integer, Got> heroGots;

  @NoteField(value = "食灵原型ID")
  public HashSet<Integer> heroProtoGots;

  @NoteField(value = "食灵原型ID,升星领取情况")
  public Map<Integer, StarRewardStatus> heroStarGots;

  @NoteField("<星级，bool>")
  public Map<Integer, Boolean> allStarGots;

//    @NoteField(value = "已经激活的英雄羁绊  Map<羁绊tid, 羁绊数据>")
//    public Map<Integer, YokeGot> yokeGots;

  @Override
  public void initialize(Role role) {
    super.initialize(role);
    if (heroGots == null) {
      heroGots = new HashMap<>();
    }
    if (heroProtoGots == null) {
      heroProtoGots = new HashSet<>();
    }
    if(heroStarGots == null){
      heroStarGots = new LinkedHashMap<>();
    }
    if (allStarGots == null) {
      allStarGots = new HashMap<>();
    }
//        if(yokeGots == null){
//            yokeGots = new HashMap<>();
//        }
  }

  @Override
  public void onEnter() {
    if (allStarGots.isEmpty()) {
      for (Hero hero : role.getBag().hero.getList()) {
        HeroBaseRow baseRow = hero.getTpl(role.getGame());
        if (baseRow != null && !allStarGots.containsKey(baseRow.star)) {
          allStarGots.put(baseRow.star, true);
        }
      }
    }
  }

//    public int calcScore(){
//        return heroGots.size() + yokeGots.values().stream().mapToInt(e -> e.actives.size()).sum();
//    }

  /**
   * 获得新英雄尝试添加
   */
  public void addHero(int heroTid, int protoId) {
    heroProtoGots.add(protoId);
    GainHeroRwdRow row = role.getGame().table.heroIntroduction.heroRwd.get(heroTid);
    if (row == null) {
      return;
    }
    Got heroGot = heroGots.get(heroTid);
    if (heroGot == null) {
      heroGot = new Got();
      heroGot.heroTid = heroTid;
      heroGot.active = false;
      heroGots.put(heroTid, heroGot);
      // 如果有变更就通知前端
      role.sendNow(new HeroIntroductionAddResp(heroTid));
    }
    HeroBaseRow hero = game().table.hero.base.get(heroTid);
    if (hero != null && !allStarGots.containsKey(hero.star)) {
      allStarGots.put(hero.star, true);
      role.sendNow(new AllStarGetResp(allStarGots));
    }
  }

  /**
   * 领取英雄奖励
   */
  public EcResult<Void> getHeroRwd(int heroTid) {
    EcResult<Void> r = new EcResult<>();
    Got heroGot = heroGots.get(heroTid);
    if (heroGot == null) {
      return r.fail("尚未未获得英雄");
    }
    if (heroGot.active) {
      return r.fail("奖励已领取");
    }
    GainHeroRwdRow row = role.getGame().table.heroIntroduction.heroRwd.get(heroTid);
    if (row == null) {
      return r.fail("奖励不存在");
    }
    heroGot.active = true;
    MixRes rwd = new MixRes(row.rewardStr);
    rwd.add(role, true, 0);
    role.getGame().notice.dialogReward(role, rwd);
    role.sendNow(new GetHeroRwdResp(heroTid));
    return r.success();
  }

//    public void activeYoke(int tid, int protoId){
//        YokeGot yokeGot = yokeGots.get(tid);
//        if(yokeGot != null && (yokeGot.active || yokeGot.actives.contains(protoId))){
//            role.getGame().notice.message(role,"已经激活过该羁绊");
//            return;
//        }
//        HeroYokeRow yokeRow = role.getGame().table.heroIntroduction.heroYoke.get(tid);
//        if(yokeRow == null){
//            role.getGame().notice.message(role,"错误的羁绊图鉴ID");
//            return;
//        }
//        List<Integer> herosList = yokeRow.herosList;
//        if(!herosList.contains(protoId) || !heroGots.containsKey(protoId)){
//            role.getGame().notice.message(role,"不可激活该羁绊");
//            return;
//        }
//        if(yokeGot == null){
//            yokeGot = new YokeGot();
//            yokeGot.tid = tid;
//            yokeGot.actives = new ArrayList<>();
//            yokeGots.put(tid, yokeGot);
//        }
//        yokeGot.actives.add(protoId);
//        yokeGot.active = yokeGot.actives.size() == herosList.size();
//        role.sendNow(new HeroYokeActiveResp(tid, protoId));
//        role.getBase().updatePower();
//    }

//    @JsonIgnore
//    public void getYokeReward(int tid){
//        YokeGot yokeGot = yokeGots.get(tid);
//        if(yokeGot == null){
//            role.getGame().notice.message(role,"请先激活羁绊");
//            return;
//        }
//        if(yokeGot.got){
//            role.getGame().notice.message(role,"已经领取过该奖励");
//            return;
//        }
//        HeroYokeRow yokeRow = role.getGame().table.heroIntroduction.heroYoke.get(tid);
//        if(yokeRow == null){
//            role.getGame().notice.message(role,"错误的羁绊图鉴ID");
//            return;
//        }
//        yokeGot.got = true;
//        MixRes reward = new MixRes(yokeRow.rewardStr);
//        reward.add(role, true ,0);
//        role.getGame().notice.dialogReward(role, reward);
//        role.sendNow(new HeroYokeGetRewardResp(tid));
//    }

  @JsonIgnore
  public FightAttr getAttr() {
    FightAttr attr = new FightAttr();
//        Map<Integer, YokeGot> yokeMap = yokeGots;
//        if(!yokeMap.isEmpty()){
//            for(YokeGot yoke : yokeMap.values()){
//                if(!yoke.actives.isEmpty()){
//                    HeroYokeRow yokeRow = role.getGame().table.heroIntroduction.heroYoke.get(yoke.tid);
//                    if(yokeRow != null){
//                        attr.add(new FightAttr(yokeRow.attrStr).scale(yoke.actives.size()));
//                    }
//                }
//            }
//        }
    return attr;
  }

//    //图鉴收集进度
//    public double collectRate() {
//        double total = role.getGame().table.heroIntroduction.heroYoke.totalHeroNum;
//        if (total > 0) {
//            double yokeHeroNum = yokeGots.values().stream().mapToInt(a -> a.actives.size()).sum();
//            return yokeHeroNum / total;
//        }
//        return 0;
//    }

  public void clearAll() {
    heroGots.clear();
//        yokeGots.clear();
  }

  public void noticeUpdate() {
//        role.sendNow(new HeroYokeDataResp(this));
  }
}
